import axios from 'axios';
// import Vue from "vue";
import { BASEURL } from '@/config';
// let router = typeof $nuxt != "undefined" ? $nuxt.$router : null;
export let $get = (url, params, headers = {}, noBase = false) => {
	if (!headers) headers = {};
	//headers['access-control-allow-origin'] = '*';
	if (typeof localStorage == 'undefined') {
		headers.Authorization = '';
	} else {
		headers.Authorization = localStorage.getItem('Authorization');
	}
	let res;
	if (!noBase) {
		res = axios.get(BASEURL + url, {
			params,
			headers,
		});
	} else {
		res = axios.get(url, {
			params,
			headers,
		});
	}
	res.then((r) => {
		// if (url.indexOf("info") != -1) {
		// }
		if (r.data.code == 403) {
			// var pathname = window.location.pathname ;
			// var search =window.location.search;
			// setTimeout(() => {
			//   router = typeof $nuxt != "undefined" ? $nuxt.$router : null;
			//   if(pathname != '' && pathname != undefined){
			//     router.push("/user/login?pathname="+pathname+"&search="+search.replace(/&/g,'@'));//把url中的&替换为@方便传参
			//   }else{
			//     router.push("/user/login");
			//   }
			// }, 1000);
		}
	});
	return res;
};

export let $post = (url, params, config) => {
	// console.log("xxxl post 1");
	// console.log(localStorage);
	// console.log("xxxl post 2");

	// if(!headers) headers = {};
	// if(typeof(localStorage) == "undefined"){
	//   headers.Authorization = ""
	// }else{
	//   headers.Authorization = localStorage.getItem('Authorization')
	// }

	// console.log(document.domain);
	// localStorage.clear();

	let res = axios.post(BASEURL + url, params, {
		headers: {
			Authorization: localStorage.getItem('Authorization'),
		},
		...config,
	});
	res.then((r) => {
		if (r.data.code == 403) {
			// if (router) router.push("/user/login");
		}
	});
	// res.then(r => {
	//   console.log("hahahaaaaa", r.data);
	//   if (data.msg.indexOf("登录") != -1 || data.msg.indexOf("失效") != -1) {
	//     const router = $nuxt.$router;
	//     console.log(router.push("/user/login"));
	//   }
	// });

	return res;
};
